package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxCommentModel;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @author liguoliang
 * @date 2018/5/23 16:05
 */
@Controller
@RequestMapping("/system/wwdx_comment")
public class WwdxCommentController extends BaseController{

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView chapterlist(WwdxCommentModel model,
                                    @RequestParam(value = "page", defaultValue = "1") int page,
                                    @RequestParam(value = "rows",defaultValue = "10") int rows) {

        model.setRest(false);
        ModelAndView mav = new ModelAndView();
        mav.addObject("listSize", wwdxCommentService.selectModelCount(model));
        mav.addObject("list", wwdxCommentService.selectModelList(model,page,rows));
        return mav;
    }

    /**
     * 修改时间 | 回复评论 保存
     */
    @RequestMapping("/save")
    public String save(WwdxCommentModel model) {
        Integer editType = model.getEditType();
        //修改时间
        if (editType == 1){
            wwdxCommentService.update(model);
            //回复评论
        }else {
            Long id = model.getId();
            WwdxCommentModel wwdxCommentModel = wwdxCommentService.getById(id);
            wwdxCommentModel.setStatus(1);
            wwdxCommentService.update(wwdxCommentModel);
            model.setStatus(0);
            model.setRecommend(0);
            model.setPid(id);
            model.setGmtCreated(new Date());
            wwdxCommentService.save(model);
        }
        return "redirect:/system/wwdx_comment/list";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long id,Integer editType) {
        ModelAndView mav = new ModelAndView();
        WwdxCommentModel wwdxCommentModel = wwdxCommentService.getById(id);
        wwdxCommentModel.setEditType(editType);
        mav.addObject("model", wwdxCommentModel);
        if (editType == 0){
            mav.addObject("list",wwdxCommentService.getReplys(id));
        }
        return mav;
    }

    /**
     * 修改推荐保存
     */
    @RequestMapping("/update")
    public String update(Long id,Integer recommend) {
        WwdxCommentModel wwdxCommentModel = wwdxCommentService.getById(id);
        wwdxCommentModel.setRecommend(recommend);
        wwdxCommentService.update(wwdxCommentModel);
        return "redirect:/system/wwdx_comment/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(Long id) {
        wwdxCommentService.delete(wwdxCommentService.getById(id));
        List<WwdxCommentModel> wwdxCommentModels = wwdxCommentService.getReplys(id);
        wwdxCommentModels.forEach(wwdxCommentService::delete);
        return "redirect:/system/wwdx_comment/list";
    }
}